package com.ebei.admin.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ebei.admin.common.util.AuthRealm;
import com.ebei.admin.common.util.ConvertUtil;
import com.ebei.admin.mapper.SysOrgMapper;
import com.ebei.admin.mapper.SysParkMapper;
import com.ebei.admin.model.dto.OrgDTO;
import com.ebei.admin.model.entity.SysCompany;
import com.ebei.admin.model.entity.SysOrg;
import com.ebei.admin.model.entity.SysPark;
import com.ebei.admin.model.entity.SysUser;
import com.ebei.admin.service.SysCompanyService;
import com.ebei.admin.service.SysParkService;
import com.ebei.admin.service.SysUserService;
import com.ebei.common.constant.CommonConstant;
import com.ebei.common.util.Query;
import com.ebei.common.util.Result;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class SysParkServiceImpl extends ServiceImpl<SysParkMapper, SysPark> implements SysParkService {

    @Autowired
    private SysParkMapper sysParkMapper;
    @Autowired
    private SysOrgMapper sysOrgMapper;
    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private SysCompanyService sysCompanyService;

    @Override
    public Page findByParam(Query<Map<String, Object>> query) {
        Map<String, Object> map = query.getCondition();
        //不是超管,没有园区列表模块
        if (AuthRealm.isSpAdmin()) {
            query.setRecords(sysParkMapper.findByParam(query, map));
        }
        return query;
    }

    @Override
    public Result<Boolean> update(Map<String, Object> map) throws Exception {
        SysPark sysPark = (SysPark) ConvertUtil.mapToObject(map, SysPark.class);
        sysPark.setUpdateTime(new Date());
        sysParkMapper.updateById(sysPark);
        SysOrg sysOrg = new SysOrg();
        BeanUtils.copyProperties(sysPark, sysOrg);
        sysOrgMapper.updateById(sysOrg);
        return Result.OK;
    }

    @Override
    public Result findUnitByType(Map<String, Object> map) {
        String orgId = String.valueOf(map.get("orgId"));
        SysOrg sysOrg = sysOrgMapper.selectById(orgId);
        if (sysOrg != null) {
            map.put("levelNum", sysOrg.getLevelNum().substring(0, CommonConstant.ORGTYPE_MAX_SIZE.length()));
        }
        return new Result(sysParkMapper.findUnitByType(map), Result.SUCCESS);
    }

    @Override
    public Result findByUserId(Integer userId) {
        return new Result(sysParkMapper.findByUserId(userId), Result.SUCCESS, "操作成功");
    }

    @Override
    public Result delete(Integer id) {
        SysPark sysPark = sysParkMapper.selectById(id);
        sysPark.setDelFlag(CommonConstant.STATUS_DEL);
        sysParkMapper.updateById(sysPark);
        return Result.OK;
    }


    @Override
    public Result lock(OrgDTO orgDTO) {
        // 查询园区管理员账户
        SysPark sysPark = sysParkMapper.selectById(orgDTO.getOrgId());
        SysUser condition = new SysUser();
        condition.setUserAccount(sysPark.getAdminAccount());
        condition.setDelFlag(CommonConstant.STATUS_NORMAL);
        SysUser adminUser;
        try {
            adminUser = sysUserService.selectOne(new EntityWrapper<>(condition));
        } catch (Exception e){
            return new Result(Result.FAIL,"数据错误");
        }

        // 锁定管理员账号
        adminUser.setDelFlag(orgDTO.getDelFlag());
        adminUser.setUpdateTime(new Date());
        sysUserService.updateById(adminUser);
        return new Result(Result.SUCCESS,"已禁用");
    }

    @Override
    public Result getParkList() {
        List<SysPark> result = new ArrayList<>();
        // 超级管理员
        if(AuthRealm.isSpAdmin()){
            SysPark condition = new SysPark();
            condition.setDelFlag(CommonConstant.STATUS_NORMAL);
            result = this.selectList(new EntityWrapper<>(condition));
        }else if(AuthRealm.isGov()){
            result.add(this.selectById(AuthRealm.getPrincipal().getOrgId()));
        }else if(AuthRealm.isCom()){
            SysCompany sysCompany = sysCompanyService.selectById(AuthRealm.getPrincipal().getOrgId());
            result.add(this.selectById(sysCompany.getParkId()));
        }

        return new Result(result,Result.SUCCESS,"获取成功");
    }

}
